import 'dart:async';
import 'dart:developer';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'ahu_text.dart';
import 'chat_net_image.dart';


class PermissionCheck{
 static Future<PermissionStatus> checkLocationPermission() async {
    final status = await Permission.locationWhenInUse.status;
    return status;
  }
}

class AllDialog {
  static AllDialog? _instance;

  static var totalDuration = 60.obs;

  static AllDialog get instance {
    _instance ??= AllDialog._();
    return _instance!;
  }

  AllDialog._();

  static late OverlayEntry entry;

  static bool _isShow = false;

  static bool get isShow => _isShow;

  // 定时器,计算通话时长
  static Timer? downTimer;

  show(
    BuildContext context, {
    String? content,
    required GestureTapCallback onTap,
  }) {
    _insertOverlay(context, content: content, onTap: onTap);
  }

  // 悬浮按钮，可以拖拽（可自定义样式）
  void _insertOverlay(
    BuildContext context, {
    String? content,
    required GestureTapCallback onTap,
  }) {
    entry = OverlayEntry(builder: (context) {
      return Positioned(
        top: ScreenUtil().statusBarHeight,
        left: 0,
        width: ScreenUtil().screenWidth,
        height: 120,
        child: VideoPushWidget(
          content: content,
          onTap: () => onTap(),
        ),
      );
    });
    _isShow = true;
    Overlay.of(context).insert(entry);
  }

  static dissmiss() {
    // AhuAudioPlayer.instance.stopAudioPlay();
    totalDuration.value = 60;
    downTimer?.cancel();
    _isShow = false;
    entry.remove();
  }
}

// 权限弹窗
Future<String> permissionDialog(
  BuildContext context, {
  required String title,
  required String content,
}) async{
   var action = await showCupertinoDialog(
    barrierDismissible: true,
    context: context,
    builder: (context) {
      return CupertinoAlertDialog(
        title: Text(title),
        content: Text(content),
        actions: [
          CupertinoDialogAction(
            child: const Text(
              "不允许",
              style: TextStyle(
                color: Colors.blue,
                fontSize: 18,
                fontWeight: FontWeight.w600,
              ),
            ),
            onPressed: () {
              Navigator.of(context).pop('不允许');
            },
          ),
          CupertinoDialogAction(
            child: const Text("好",
                style: TextStyle(
                    color: Colors.blue,
                    fontSize: 18,
                    fontWeight: FontWeight.w600)),
            onPressed: () {
              Navigator.of(context).pop('允许');
            },
          ),
        ],
      );
    },
  );
 return action;
}

// ignore: must_be_immutable
class VideoPushWidget extends StatelessWidget {
  final String? path;
  final String? name;
  final String? content;
  String? type;
  final GestureTapCallback? onPress;
  final GestureTapCallback? onAccept;
  final GestureTapCallback? onTap;

  VideoPushWidget({
    Key? key,
    this.path,
    this.name,
    this.content,
    this.type,
    this.onPress,
    this.onAccept,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: InkWell(
        onTap: () {
          if (onTap != null) {
            onTap!();
          }
        },
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 22.w),
          padding: EdgeInsets.symmetric(horizontal: 22.w),
          decoration: BoxDecoration(
              color: const Color(0x9F212121),
              borderRadius: BorderRadius.circular(12.w)),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ChatNetImage(
                imageUrl: '$path',
                width: 40,
                height: 40,
                fit: BoxFit.cover,
                radius: 6,
              ),
              SizedBox(width: 4.w),
              Expanded(
                  child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  AhuText(
                    text: '$name',
                    fontSize: 14,
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                  SizedBox(height: 4.w),
                  AhuText(
                    text: '$content',
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                ],
              )),
              SizedBox(width: 18.w),
              InkWell(
                onTap: () {
                  if (onPress != null) {
                    onPress!();
                  }
                },
                child: Image.asset(
                  'assets/images/chat/chat_cancel.png',
                  width: 40,
                  height: 40,
                  package: 'ahu_pub_lib',
                ),
              ),
              SizedBox(width: 18.w),
              InkWell(
                onTap: () {
                  if (onAccept != null) {
                    onAccept!();
                  }
                },
                child: Image.asset(
                  type == 'ChannelType.video'
                      ? 'assets/images/chat/video_accept.png'
                      : 'assets/images/chat/chat_accept.png',
                  width: 40,
                  height: 40,
                  package: 'ahu_pub_lib',
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
